import { getToken } from '@/utils/index.js'
import { Message } from '@arco-design/web-vue'
import axios from 'axios'

axios.defaults.headers.post['Content-Type'] = 'application/json'
const request = axios.create({
  // baseURL: 'http://canvas-tools-api.jdxlink.com', // url = base url + request url
})

// request interceptor
request.interceptors.request.use(
  config => {
    config.headers.Token = getToken()
    config.headers['m-app-id'] = 'OTEZ6KRE'
    return config
  },
  error => {
    console.log(error)
    Message.error(error)
    return Promise.reject(error)
  }
)

// response interceptor
request.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data
    if (!res.code && !res.message && !res.data) {
      return res
    }
    if (!res.success) {
      Message.error(res.message)
      return Promise.reject(res)
    }
    return res
  },
  error => {
    console.log('err' + error) // for debug
    Message.error(error)
    return Promise.reject(error)
  }
)

export default request
